package Models;


import javax.swing.table.*;
import javax.swing.event.*;

import Data.GrammarRule;

import java.util.LinkedList;


public class GrammarTableModel implements TableModel
{
	 
    private LinkedList<GrammarRule> grammarData = new LinkedList<GrammarRule>();
   
  
    private LinkedList<TableModelListener> listeners = new LinkedList<TableModelListener>();
   
    public GrammarTableModel(LinkedList<GrammarRule> dataValue){
    	this.grammarData=dataValue;

    }
    public int getColumnCount() {
       
        return 2;
    }
    
   
    public int getRowCount() {
       
        return grammarData.size();
    }
    
   
    public Object getValueAt(int rowIndex, int columnIndex) {
        GrammarRule aux;
        
      
        aux = (GrammarRule)(grammarData.get(rowIndex));
        
       
        switch (columnIndex)
        {
            case 0:
                return aux.getSubstitutionName();
            case 1:
                return aux.getSubstitutionValue();
        }
		return null;
    }
    
 
    public void deleteGrammar (int row)
    {
       
       this.grammarData.remove(row);
        
       
        TableModelEvent event = new TableModelEvent (this, row, row, 
         TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE);
        updateListeners (event);
       
        
        
    }
    
    
    public void addGrammar (GrammarRule newRule)
    {
      
      
        this.grammarData.add(newRule);
      
        TableModelEvent event;
        event = new TableModelEvent (this, this.getRowCount()-1,
            this.getRowCount()-1, TableModelEvent.ALL_COLUMNS,
            TableModelEvent.INSERT);

        
        updateListeners (event);
    }
    
    
    public void addTableModelListener(TableModelListener l) {
        listeners.add (l);
    }
    
  
    public Class getColumnClass(int columnIndex) {
     switch (columnIndex)
        {
            case 0:
               
                return String.class;
            case 1:
                return String.class;
        }
		return null;
    }
   
    public String getColumnName(int columnIndex) 
    {
      
        switch (columnIndex)
        {
            case 0:
                return "Rule";
            case 1:
                return "Value";
            default:
                return null;
        }
    }
    
   
    public boolean isCellEditable(int rowIndex, int columnIndex) {
      
        return true;
    }
  
    public void removeTableModelListener(TableModelListener l) {
       
        listeners.remove(l);
    }
    
   
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) 
    {
        
        GrammarRule aux;
        aux = (GrammarRule)(grammarData.get(rowIndex));
       
        switch (columnIndex)
        {
            case 0:
                aux.setSubstitutionName((String)aValue);
                break;
            case 1:
                aux.setSubstitutionValue((String)aValue);
                break;
        }
        
       
        TableModelEvent evento = new TableModelEvent (this, rowIndex, rowIndex, 
            columnIndex);
        updateListeners (evento);
    }
    
   
    private void updateListeners (TableModelEvent evento)
    {
        int i;
        
        
        for (i=0; i<listeners.size(); i++)
            ((TableModelListener)listeners.get(i)).tableChanged(evento);
    }
    
}